package com.wimcorp.magic.abilities;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.color.Color;
import com.wimcorp.magic.input.ColorInput;

/**
 * example {colorOptions:['BLACK'],amount:'2'}
 * 
 * colorOptions options : BLACK,RED,WHITE,GREEN,BLUE,NEUTRAL,ANY
 */
@Entity
@Table(name = "addmanatopool", schema = "abilities")
public class AddManaToPool extends Ability {

	@ElementCollection
	@CollectionTable(name = "addmanatopool_coloroptions", schema = "abilities", joinColumns = @JoinColumn(name = "addmanatopool_id"))
	private List<String> colorOptions = new ArrayList();

	private int amount;

	protected AddManaToPool() {
	}

	public AddManaToPool(AbilityTrigger trigger) {
		super(trigger);
	}

	public AddManaToPool(AddManaToPool ability) {
		super(ability);
		this.amount = ability.getAmount();
		this.colorOptions = ability.getColorOptions();
	}

	@Override
	protected void executeAbility() {
		Color[] colors = new Color[amount];
		for (int i = 0; i < colors.length; i++) {
			if (colorOptions.size() == 1) {
				if (ColorInput.ANY.toString().equals(colorOptions.get(0))) {
					try {
						int selectedColor = getPlayer().getUserInterface().selectColor(new ArrayList<String>());
						colors[i] = Color.values()[selectedColor];
					} catch (RemoteException e) {

						e.printStackTrace();
					}
				} else {
					colors[i] = Color.valueOf(colorOptions.get(0));
				}
			} else {
				try {
					int selectedColor = getPlayer().getUserInterface().selectColor(colorOptions);
					colors[i] = Color.values()[selectedColor];
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
		getPlayer().conjureMana(colors);
	}

	@Override
	public AddManaToPool clone() {

		return new AddManaToPool(this);
	}

	public int getAmount() {
		return amount;
	}

	public List<String> getColorOptions() {
		return colorOptions;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public void setColorOptions(List<String> colorOptions) {
		this.colorOptions = colorOptions;
	}

}
